package com.uni.utils;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.util.StringUtils;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.uni.db.FileService;
import com.uni.domain.File;

public class FileUtils {

	public static File parseFile(CommonsMultipartFile commonsFile, String type, FileService dao){
		if (commonsFile == null) return null;
		if (commonsFile.isEmpty()) return null;
        File file = new File();
        file.setData(commonsFile.getBytes());
        file.setContentType(commonsFile.getContentType());
        file.setType(type);
        System.out.println(commonsFile.getOriginalFilename());
        file.setFileName("`" + commonsFile.getOriginalFilename() + "`");
		return file;
	}
	
	public static Set<File> parseFiles(CommonsMultipartFile[] commonsFiles, String type, FileService dao){
		if(commonsFiles == null){
			return Collections.emptySet();
		}
		Set<File> files = new HashSet<File>();
		for(CommonsMultipartFile cf : commonsFiles){
			files.add(parseFile(cf, type, dao));
		}
		return files;
	}
	
	public static Set<File> parseFiles(Object[] commonsFiles, String type, FileService dao) throws Exception {
		if(commonsFiles == null){
			return Collections.emptySet();
		}
		Set<File> files = new HashSet<File>();
		for(Object cf : commonsFiles){
			File f = parseFile((CommonsMultipartFile)cf, type, dao);
			if(f != null) files.add(f);
		}
		return files;
	}

}
